package com.letou.ls.view;

import java.util.ArrayList;

import com.letou.ls.uitl.Logger;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableBadyView2 extends TableLayout {

	private ArrayList<ArrayList<ArrayList<String>>> mDataList;
	private int cellBorder = 1;
	private int tableTextColor = Color.BLACK;
	private int tableBackgroundAColor = Color.WHITE;
	private int tableBackgroundCColor = Color.parseColor("#ccffff");
	private int tableBackgroundDColor = Color.parseColor("#cacdd5");
	private Typeface tableTypeface = Typeface.SANS_SERIF;
	private int tableGravity = Gravity.CENTER;
	private float mLeafTextSize;
	private ArrayList<Float> mWidthList;

	public TableBadyView2(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public TableBadyView2(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TableBadyView2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
	}

	public void addData(ArrayList<ArrayList<ArrayList<String>>> datalist, ArrayList<Float> widthList, float textSize) {
		this.setBackgroundColor(Color.RED);
		this.setBaselineAligned(true);

		mDataList = datalist;
		mWidthList = widthList;
		mLeafTextSize = textSize;
		drawTableBody();
		invalidate();
	}

	public ArrayList<ArrayList<ArrayList<String>>> get_data() {
		return mDataList;
	}

	public void drawTableBody() {
		/**
		 * 画表内容
		 */
		if (mDataList != null) {
			int size = mDataList.size();
			for (int i = 0; i < size; i++) {
				TableRow row = new TableRow(this.getContext());

				row.setPadding(0, 0, 0, cellBorder);
				int ss = mDataList.get(i).size();
				for (int j = 0; j < ss; j++) {

					String label = mDataList.get(i).get(j).get(0);

					String type = null;
					if (mDataList.get(i).get(j).size() == 2) {
						type = mDataList.get(i).get(j).get(1);
					}

//					Logger.v("lss", "sign    " + type + "   " + label);
					TextView dataCell = new TextView(this.getContext());

					if ("1".equals(type)) {

						dataCell.setTextColor(Color.RED);
						dataCell.setBackgroundColor(tableBackgroundAColor);
					} else if ("2".equals(type)) {

						dataCell.setTextColor(Color.RED);
						dataCell.setBackgroundColor(tableBackgroundCColor);
					} else if ("3".equals(type)) {

						dataCell.setTextColor(tableTextColor);
						dataCell.setBackgroundColor(tableBackgroundCColor);
					} else if ("4".equals(type)) {

						dataCell.setTextColor(tableTextColor);
						dataCell.setBackgroundColor(tableBackgroundDColor);
					} else if ("5".equals(type)) {

						dataCell.setTextColor(Color.RED);
						dataCell.setBackgroundColor(tableBackgroundDColor);
					} else {
						dataCell.setTextColor(tableTextColor);
						dataCell.setBackgroundColor(tableBackgroundAColor);
					}

					// if (i % 2 == 0) {
					// dataCell.setBackgroundColor(tableBackgroundSColor);
					// } else {
					// dataCell.setBackgroundColor(tableBackgroundQColor);
					// }
					dataCell.setTypeface(tableTypeface);
					if (j != 0) {
						dataCell.setGravity(tableGravity);
					} else {
						dataCell.setGravity(Gravity.LEFT);
					}
					// int height;
					dataCell.setTextSize(mLeafTextSize);
					// height = (int) (mLeafTextSize * 1.5 * density);
					// dataCell.setHeight(height);
					dataCell.setText(label);
					if (mWidthList.size() > j) {
						dataCell.setWidth(mWidthList.get(j).intValue());
					} else {
						// dataCell.setWidth(baseW);
					}

					TableRow.LayoutParams t = new TableRow.LayoutParams();
					t.setMargins(0, 0, 1, 0);
					dataCell.setPadding(1, 0, 1, 0);
					dataCell.setLayoutParams(t);

					row.addView(dataCell);
				}
				this.addView(row);
			}
		}
	}

}
